package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.GoodsType;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.util.PageResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.w3c.dom.stylesheets.LinkStyle;

import java.util.List;
import java.util.Map;

/**
 * @description 商品信息Controller
 * /goodsType/loadGoodsType
 */

@RestController
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 分页查询商品库存信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param codeOrName 商品编码或名称
     * @param goodsTypeId 商品类别ID
     * @return
     */
    //http://localhost:8080/goods/listInventory post
    @PostMapping("listInventory")
    public Map<String,Object> getStockInfoPage(Integer page,Integer rows,String codeOrName,Integer goodsTypeId){
        // 先不设条件查询
        // select * from t_goods limit(1,10)
        Map<String, Object> stockInfoPage = goodsService.getStockInfoPage(page,rows,codeOrName,goodsTypeId);
        return stockInfoPage;
    }


    /**
     * 分页查询商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param goodsName 商品名称
     * @param goodsTypeId 商品类别ID
     * @return
     */
    @PostMapping("list")
    public PageResult getGoodsInfoByPage(Integer page,Integer rows,String goodsName,Integer goodsTypeId){
        return goodsService.getGoodsInfoByPage(page,rows,goodsName,goodsTypeId);
    }

    /**
     * 生成商品编码
     * @return
     */
    @RequestMapping("/getCode")
    @RequiresPermissions(value = "商品管理")
    public ServiceVO getCode() {
        return goodsService.getCode();
    }

    /**
     * 添加或修改商品信息 http://localhost:8080/goods/save?goodsId=37
     * @param goods 商品信息实体
     * @return
     */
    @PostMapping("/save")
    public ServiceVO save(Goods goods,Integer goodsId){
        if (goodsId != null){
            // 有id -》 修改
            goodsService.updateGoods(goods,goodsId);
        }else {
            // 没有id -》 保存
            goodsService.insertGoods(goods);
        }
        return new ServiceVO(100, "保存商品信息成功");
    }

    /**
     * 删除商品信息 http://localhost:8080/goods/delete
     * @param goodsId 商品ID
     * @return
     */
    @PostMapping("delete")
    public ServiceVO deleteGoodsInfoById(Integer goodsId){

        int i = goodsService.deleteGoodsInfoById(goodsId);
        if (i>0){
            //删除成功
            return new ServiceVO(100,"删除成功");
        }else {
            // 删除失败
            return new ServiceVO(200,"不能删除有单据的");
        }
    }
    /**
     * 分页查询无库存商品信息 http://localhost:8080/goods/getNoInventoryQuantity
     * @param page 当前页
     * @param rows 每页显示条数
     * @param nameOrCode 商品名称或商品编码
     * @return
     */
    @PostMapping("getNoInventoryQuantity")
     public PageResult getNoInventoryQuantity(Integer page,Integer rows,String nameOrCode){
         PageInfo<Goods> noInventorypageInfo = goodsService.getNoInventoryByPage(page,rows,nameOrCode);
         return new PageResult(noInventorypageInfo.getTotal(),noInventorypageInfo.getList());
     }

    /**
     * 分页查询有库存商品信息  http://localhost:8080/goods/getHasInventoryQuantity
     * @param page 当前页
     * @param rows 每页显示条数
     * @param nameOrCode 商品名称或商品编码
     * @return
     */
    @PostMapping("getHasInventoryQuantity")
    public PageResult getHasInventoryQuantity(Integer page,Integer rows,String nameOrCode){
        PageInfo<Goods> hasInventorypageInfo = goodsService.getHasInventoryByPage(page,rows,nameOrCode);
        return new PageResult(hasInventorypageInfo.getTotal(),hasInventorypageInfo.getList());
    }

    /**
     * 添加商品期初库存 http://localhost:8080/goods/saveStock?goodsId=25
     * @param goodsId 商品ID
     * @param inventoryQuantity 库存
     * @param purchasingPrice 成本价
     * @return
     */
    @PostMapping("saveStock")
    public ServiceVO saveStocks(Integer goodsId,Integer inventoryQuantity,double purchasingPrice){
//        if (inventoryQuantity != null){
            // 有inventoryQuantity -》 修改
            goodsService.updateGoodsStock(goodsId,inventoryQuantity,purchasingPrice);
//        }else {
//            // 没有inventoryQuantity -》 保存
//            goodsService.insertGoodsStocks(inventoryQuantity,purchasingPrice);
//        }
        return new ServiceVO(100, "保存商品信息成功");
    }
    /**
     * 删除商品库存 http://localhost:8080/goods/deleteStock
     * @param goodsId 商品ID
     * @return
     */
    @PostMapping("deleteStock")
    public ServiceVO deleteGoodsStock(Integer goodsId){
        int i = goodsService.deleteGoodsStock(goodsId);
        if (i>0){
            //删除成功
            return new ServiceVO(100,"删除库存成功");
        }else {
            // 删除失败
            return new ServiceVO(200,"不能删除有单据的");
        }
    }
    /**
     * 查询库存报警商品信息 http://localhost:8080/goods/listAlarm
     * @return
     */
     @PostMapping("listAlarm")
    public Map<String,Object> getListAlarm(){
         return goodsService.getListAlarm();
     }

}
